package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHUtil
import cn.com.kh.signUtil
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * 工单查询API
 */

public class apiGetWODetail implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiGetWODetail.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        long startTime = System.currentTimeMillis();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");

        // 初始化返回参数
        JSONObject result = new JSONObject();

        JSONObject resultJson = null;
        //接收客户端调用参数
        String callJson = "";
        InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
        BufferedReader iny = new BufferedReader(reader);
        String line;
        while ((line = iny.readLine()) != null) {
            callJson += line;
        }

        log.info("[调用请求信息]:" + callJson);

        JSONObject signJson = signUtil.CheckSign(JSONObject.parseObject(callJson));
        //log.info("[签名验证结果]:");
        //log.info(signJson);
        //if (signJson != null) {
		if (1==2) {
            return signJson.toString();
        } else {
            try {
                //解析Json
                JSONObject inJson = JSONObject.parseObject(callJson);

                //获取查询参数
                String s_workorder_guid = inJson.getJSONObject("data").getString("s_workorder_guid");// 输入的工单guID
                String s_elitetask_guid = inJson.getJSONObject("data").getString("s_elitetask_guid");// 输入的工单任务guID
                String tabcname = inJson.getJSONObject("data").getString("tabcname");// 工单tabc表名

                log.debug("请求参数-查询条件-s_workorder_guid:" + s_workorder_guid);

                String q_WhereSQL = " Where Fm.Category <> 'SALE01' and Fm.Map_id<>'8IAUGM'  And Eta.Obflag = 0";
                log.debug("初始化控制查询条件SQL:" + q_WhereSQL);

                // 模糊查询条件处理
                if (!KHUtil.isNull(s_workorder_guid)) {
                    q_WhereSQL = q_WhereSQL + " And Eta.objective_guid = '" + s_workorder_guid + "'";
                }

                q_WhereSQL = q_WhereSQL + "and rownum= 1";
                log.debug("控制查询条件完成SQL:" + q_WhereSQL);
				//工单详情
                String queryWorkOrderSQL ="select * from objective where objective_guid='"+s_workorder_guid+"'";
                String queryElitetaskSQL ="select * from elitetask where elitetask_guid='"+s_elitetask_guid+"' and objective_guid='"+s_workorder_guid+"'";
                String queryTabcSQL ="select * from "+tabcname+" where relation_id='"+s_workorder_guid+"'";


                log.debug("执行工单详情查询objective数据SQL:" + queryWorkOrderSQL);
                log.debug("执行工单详情查询elitetask数据SQL:" + queryElitetaskSQL);
                //log.debug("执行工单详情查询"+tabctablename+"数据SQL:" + queryTabcSQL);

                List WorkOrderList = eliteBean.chaxun_sql(queryWorkOrderSQL, "dataSource");
                List ElitetaskList = eliteBean.chaxun_sql(queryElitetaskSQL, "dataSource");
                List TabcList = eliteBean.chaxun_sql(queryTabcSQL, "dataSource");
                //获取总行数
                log.debug("执行工单详情查询objective数据SQL获取总条数:" + WorkOrderList.size());
                log.debug("执行工单详情查询elitetask数据SQL获取总条数:" + ElitetaskList.size());
                //log.debug("执行工单详情查询"+tabctablename+"数据SQL获取总条数:" + TabcList.size());
                Map tabcMap=TabcList.get(0);
                net.sf.json.JSONObject tabcJson=new net.sf.json.JSONObject();
                for(String key:tabcMap.keySet()){
                    tabcJson.put(key,tabcMap.get(key).toString());
                }

                String reJoson = Util.getJsonFromList(WorkOrderList);
                log.debug("objective转换前："+WorkOrderList);
                log.debug("objective转换后："+reJoson);
                reJoson=!KHUtil.isNull(reJoson)?reJoson:"[]";
                String reJoson2 = Util.getJsonFromList(ElitetaskList);
                reJoson2=!KHUtil.isNull(reJoson2)?reJoson2:"[]";
                String reJoson3 = Util.getJsonFromList(TabcList);
                reJoson3=!KHUtil.isNull(reJoson3)?reJoson3:"[]";
				
				String q_WhereSQL1 = " Where 0 = 0";
                log.debug("初始化控制查询条件SQL:" + q_WhereSQL1);

                // 模糊查询条件处理
                if (!KHUtil.isNull(s_workorder_guid)) {
                    q_WhereSQL1 = q_WhereSQL1 + " And a.Workorder_Guid = '" + s_workorder_guid + "'";
                }

                q_WhereSQL1 = q_WhereSQL1 + " Order By DATE_FORMAT(a.modifieddate,'%Y-%m-%d %H:%i:%s') Desc";
                log.debug("控制查询条件完成SQL:" + q_WhereSQL1);
				//工单历史轨迹
                /*
				String queryWorkOrderSQL1 = "Select Distinct DATE_FORMAT(a.modifieddate,'%Y-%m-%d %H:%i:%s') As modifieddate,\n" +
                        "                a.workorderstepstatus,\n" +
                        "                swost.WORKORDERSTEPSTATUSDESC,\n" +
                        "                R1.Rolegroupdescription As old_rolegroupdescription,\n" +
                        "                S1.staffname            As old_staffname,\n" +
                        "                R2.rolegroupdescription,\n" +
                        "                S2.staffname,\n" +
                        "                Wnm1.Nodemapname        As Old_Nodemapname,\n" +
                        "                Wnm2.nodemapname,\n" +
                        "                a.comments,\n" +
                        "                a.comments_r_path\n" +
                        "  From Workorderstep3 a\n" +
                        "  Left Join Wo3_Node_Map Wnm1\n" +
                        "    On a.From_Stepidmap = Wnm1.Nodemap_Id\n" +
                        "  Left Join Wo3_Node_Map Wnm2\n" +
                        "    On a.To_Stepidmap = Wnm2.Nodemap_Id\n" +
                        "  Left Join Rolegroup R1\n" +
                        "    On a.Oldhandlegroup_Id = R1.Rolegroup_Id\n" +
						"  Left Join sysc_workorderstepstatus swost\n" +
                        "    On swost.WORKORDERSTEPSTATUS_ID = a.workorderstepstatus\n" +
						
                        "  Left Join Rolegroup R2\n" +
                        "    On a.Handlegroup_Id = R2.Rolegroup_Id\n" +
                        "  Left Join Staff S1\n" +
                        "    On a.Oldhandleby_Id = S1.Staff_Id\n" +
                        "  Left Join Staff S2\n" +
                        "    On a.Handleby_Id = S2.Staff_Id\n" + q_WhereSQL1;

                log.debug("执行工单历史查询数据SQL:" + queryWorkOrderSQL1);
                List WorkOrderList1 = eliteBean.chaxun_sql(queryWorkOrderSQL1, "dataSource");
                //获取总行数
                log.debug("执行工单历史查询数据SQL获取总条数:" + WorkOrderList1.size());
                String reJoson1 = Util.getJsonFromList(WorkOrderList1);
                reJoson1=!KHUtil.isNull(reJoson1)?reJoson1:"[]";

                 */
				
                long endTime = System.currentTimeMillis();
                result.put("code", 1);
                result.put("msg", "处理完成、调用问题处理时长(毫秒):" + (endTime - startTime));
                JSONArray dataArrObjectiveDetail = JSONArray.parseArray(reJoson);
                JSONArray dataArrElitetaskDetail = JSONArray.parseArray(reJoson2);
                JSONArray dataArrTabcDetail = JSONArray.parseArray(reJoson3);

                log.debug("objective第二次转换后："+dataArrObjectiveDetail);
                //JSONArray dataArrWOHis = JSONArray.parseArray(reJoson1);
                result.put("dataObjectiveDetail", dataArrObjectiveDetail);
                result.put("dataElitetaskDetail", dataArrElitetaskDetail);
                result.put("dataTabcDetail", tabcJson);

               // result.put("dataWOHis", dataArrWOHis);

            } catch (Exception e) {
                result.put("code", 1008);
                result.put("msg", e.getMessage());
            }

        }

        return result.toString();
    }

    //获取各步骤意见
    public JSONObject getContent(JdbcTemplate jt,String objective_guid){
        //1.
    }
}